package com.kltn.zigo.data.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.kltn.zigo.data.IP;
import com.kltn.zigo.process.JSONParser;

import android.util.Log;
import android.widget.ListView;

public class PlaceLoader {
	JSONParser jParser = new JSONParser(); 
    
	ListView list;
	
    //private static String url_all_place = IP.get_IP() +  "/zigo/KLTN/WebservicePHP/get_all_place.php";
	private static String url_all_place = IP.get_IP() +  IP.get_path_service() + "get_show_list_place.php";
 // JSON Node names
    public final String TAG_SUCCESS = "success";
    public final String TAG_PLACE = "place";
    public final String TAG_PLACE_ID = "place_id";
    public final String TAG_PLACE_NAME = "place_name";
    public final String TAG_ADDRESS = "address";
    public final String TAG_COUNT_LIKE = "count_like";
    public final String TAG_COUNT_COMMENT = "count_comment";
    public final String TAG_POINT_RATING = "point_rating";
    public final String TAG_URL_AVATAR = "url_avatar";
    public final String TAG_NAME_AVATAR = "name_avatar";
    String province_id = "";
 
    // products JSONArray
    JSONArray places = null;
    
    public PlaceLoader(String provinceID)
    {
    	province_id = provinceID;
    }
    
    public ArrayList<HashMap<String, String>> GetPlace()
    {
    	ArrayList<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("province_id", province_id));
		
		
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_place, "GET", params);

        Log.d("All Places: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                places = json.getJSONArray(TAG_PLACE);

                // looping through All Products
                for (int i = 0; i < places.length(); i++) {
                    JSONObject c = places.getJSONObject(i);

                    // Storing each json item in variable
                    String placeID = c.getString(TAG_PLACE_ID);
                    String placeName = c.getString(TAG_PLACE_NAME);
                    String countLike = "Thích: " + c.getString(TAG_COUNT_LIKE);
                    String countComment = "Bình luận: " + c.getString(TAG_COUNT_COMMENT);
                    String address = c.getString(TAG_ADDRESS);
                    String pointRating = "Điểm: " + c.getString(TAG_POINT_RATING);
                    String urlavatar = c.getString(TAG_URL_AVATAR);
                    String nameavatar = c.getString(TAG_NAME_AVATAR);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ADDRESS, address);
                    map.put(TAG_PLACE_ID, placeID);
                    map.put(TAG_PLACE_NAME, placeName);
                    map.put(TAG_COUNT_LIKE, countLike);
                    map.put(TAG_COUNT_COMMENT, countComment);
                    map.put(TAG_POINT_RATING, pointRating);
                    map.put(TAG_URL_AVATAR, urlavatar);
                    map.put(TAG_NAME_AVATAR, nameavatar);

                    // adding HashList to ArrayList
                    placesList.add(map);
                }
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return placesList;
    }
}
